package com.lingx.jt808.msg;

import com.lingx.jt808.IJT808MsgHandler;
import com.lingx.jt808.server.netty.Utils;
import com.lingx.jt808.utils.CoordinateConversion;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

@Component
public class Msg0704 extends AbstrctMsgHandler implements IJT808MsgHandler {

    private static final Logger log = LoggerFactory.getLogger(Msg0704.class.getSimpleName());

    @Override
    public int getMsgId() {
        return 0x0704;
    }

    @Override
    public String getTag() {
        return "[0704]定位数据批量上传";
    }

    @Override
    public void handle(ByteBuf data, String tid, int msgId, int msgSn, ChannelHandlerContext ctx, boolean isVersion) {
        int len = data.readUnsignedShort();
        byte type = data.readByte();
        Map<String, Object> map = new HashMap<>();
        for (int i = 0; i < len; i++) {
            int len2 = 0;
            try {
                len2 = data.readUnsignedShort();
            } catch (Exception e) {
            }
            if (len2 == 0) {
                break;
            }

            byte[] array2 = new byte[len2];
            data.readBytes(array2);
            ByteBuf data2 = Unpooled.wrappedBuffer(array2);
            long bj = data2.readUnsignedInt();
            long zt = data2.readUnsignedInt();
            double lat = data2.readUnsignedInt() / 1000000f;
            double lng = data2.readUnsignedInt() / 1000000f;
            int height = data2.readUnsignedShort();
            int speed = data2.readUnsignedShort();
            int fx = data2.readUnsignedShort();
            byte[] bytes = new byte[6];
            data2.readBytes(bytes);
            String gpstime = "20" + Utils.bytesToHex(bytes);

            // 检测数据是否有效
            if (!CoordinateConversion.checkLoLa(String.format("%.5f", lng), String.format("%.5f", lat))) {
                log.info("0704 location error {}", tid);
                continue;
            }

            DateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
            sdf.setLenient(false);
            try {
                sdf.parse(gpstime);
            } catch (Exception e) {
                log.info("0704 datetime error {}", tid);
                continue;
            }

            if (fx > 360) {
                log.info("0704 direction error {}", tid);
                continue;
            }

            if (speed >= 2000) {
                log.info("0704 speed error {}", tid);
                continue;
            }

            if (height >= 4000) {
                log.info("0704 height error {}", tid);
                continue;
            }

//            if ((zt & 0b10) != 2) {
//                log.info("0704 status location failure {}", tid);
//                continue;
//            }

            map.clear();
            map.put("tid", tid);
            map.put("alarm", bj);
            map.put("status", zt);
            map.put("lat", lat);
            map.put("lng", lng);
            map.put("height", height);
            map.put("speed", speed / 10f);
            map.put("direction", fx);
            map.put("gpstime", gpstime);

            this.databaseService.handler0704(tid, map);
        }

        if (!map.isEmpty()) {
            this.databaseService.handler0704Device(tid, map);
        }
    }
}
